package com.jiaming.restaurant.api.goods.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jiaming.restaurant.api.common.bean.contants.CommonContants;
import com.jiaming.restaurant.api.common.bean.vo.MyPageVO;
import com.jiaming.restaurant.api.goods.bean.entity.Goods;
import com.jiaming.restaurant.api.goods.bean.vo.GoodsDisableVO;
import com.jiaming.restaurant.api.goods.bean.vo.GoodsVO;
import com.jiaming.restaurant.api.goods.mapper.GoodsMapper;
import com.jiaming.restaurant.api.goods.service.IGoodsService;
import com.jiaming.restaurant.api.restaurantgoods.service.IRestaurantGoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author dragon
 * @create 2020-12-28 16:02
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods>
        implements IGoodsService {

    @Autowired
    IRestaurantGoodsService restaurantGoodsService;

    @Override
    public MyPageVO<Goods> page(GoodsVO goodsVO) {
        QueryWrapper<Goods> query = Wrappers.query();
        Integer disable = goodsVO.getDisable();
        if (disable != null) {
            query.eq("disable", disable);
        }
        String name = goodsVO.getName();
        if (StrUtil.isNotEmpty(name)) {
            query.like("name", name);
        }
        Integer minPrice = goodsVO.getMinPrice();
        if (minPrice != null) {
            query.ge("price", minPrice);
        }
        Integer maxPrice = goodsVO.getMaxPrice();
        if (maxPrice != null) {
            query.le("price", maxPrice);
        }
        String desc = goodsVO.getDesc();
        if (StrUtil.isNotEmpty(desc) && CommonContants.YES.equals(desc.toUpperCase())) {
            query.orderByDesc("price", "create_time");
        } else {
            query.orderByAsc("price", "create_time");
        }
        Page<Goods> page = new Page<>(goodsVO.getPageNum(), goodsVO.getPageSize());
        Page<Goods> goodsPage = this.page(page, query);
        MyPageVO<Goods> goodsMyPageVO = new MyPageVO<>();
        goodsMyPageVO.setItems(goodsPage.getRecords());
        goodsMyPageVO.setPageNum(goodsPage.getCurrent());
        goodsMyPageVO.setPageSize(goodsPage.getSize());
        goodsMyPageVO.setTotalItems(goodsPage.getTotal());
        goodsMyPageVO.setTotalPages(goodsPage.getPages());
        return goodsMyPageVO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean disable(GoodsDisableVO disableVO) {
        UpdateWrapper<Goods> updateWrapper = Wrappers.update();
        List<Integer> ids = disableVO.getIds();
        if (ids != null) {
            updateWrapper.in("id", ids);
        }
        Integer disable = disableVO.getDisable();
        updateWrapper.set("disable", disable);
        if (disable == 1) {
            //当商品库的商品修改为禁止销售状态时，删除所有火锅店在售的该商品信息
            restaurantGoodsService.removeByGoodsIds(ids);
        }
        boolean isOk = this.update(updateWrapper);
        return isOk;
    }
}
